<?php
	if((! isset($_REQUEST['xml'])) || ($_REQUEST['xml'] == '') || ($_REQUEST['xml'] != 1)){
		$xml = 0;
	}else{
		$xml = 1;
	}
		
	session_start();
	if(session_is_registered(username)){
		$name=$_SESSION['username'];
	}
	else{
		//header("refresh:3; url=../../home.html");
		//echo "You need to be logged in to access this page - redirecting in 3 seconds...";
		if($xml==0){
			$error['Result'] = array('DeleteLink'=>'notLoggedInError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteLink');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('notLoggedInError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	
	if((! isset($_REQUEST['link'])) || ($_REQUEST['link'] == ''))
	{
		 if($xml==0){
			$error['Result'] = array('DeleteLink'=>'noLinkInParametersError');
			$doc->formatOutput = TRUE;
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteLink');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('noLinkInParametersError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else{$link = strtolower($_REQUEST['link']);}
	
	try
	{
		$db = new PDO('sqlite:../../projecto1.db');
	}
	catch(PDOException $err)
	{
		if($xml==0){
			$error['Result'] = array('DeleteLink'=>'DatabaseError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Error');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('Database');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	
	$query = $db->prepare("SELECT * FROM Link WHERE linkURL = :link");
	$query->bindParam(":link", $link);
	$query->execute();
	$check_link = $query->fetch();
	
	if(! $check_link)
	{
		if($xml==0){
			$error['Result'] = array('DeleteLink'=>'LinkDoesntExistError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteLink');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('LinkDoesntExistError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	$idLink = $check_link['idLink'];
	
	$query = $db->prepare("SELECT * FROM User WHERE userName = :username");
	$query->bindParam(":username", $_SESSION['username'], PDO::PARAM_STR);
	$query->execute();
	$idUser = $query->fetch();
	$idUser = $idUser['idUser'];
	
	$query = $db->prepare("SELECT * FROM User_Link WHERE idUser = :idUser AND idLink = :idLink");
	$query->bindParam(":idUser", $idUser);
	$query->bindParam(":idLink", $idLink);
	$query->execute();
	$check_user_link = $query->fetch();
	
	if(! $check_user_link)
	{
		if($xml==0){
			$error['Result'] = array('DeleteLink'=>'UserDoesntHaveThatLinkError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteLink');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('UserDoesntHaveThatLinkError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	$idUserLink = $check_user_link['idUserLink'];
	
	$query = $db->prepare("DELETE FROM User_Link_Tag WHERE idUserLink = :idUserLink;");
	$query->bindParam(":idUserLink", $idUserLink);
	$query->execute();
	
	$query = $db->prepare("DELETE FROM User_Link WHERE idUserLink = :idUserLink;");
	$query->bindParam(":idUserLink", $idUserLink);
	$query->execute();
	
	if($xml==0){
		$error['Result'] = array('DeleteLink'=>'Success');
		echo json_encode($error);
	}else{
		$doc = new DomDocument('1.0');
		$doc->formatOutput = TRUE;
		// create root node
		$root = $doc->createElement('Result');
		$root = $doc->appendChild($root);
		$attr = $doc->createAttribute("xmlns:xsi");
		$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
		$root->appendChild($attr);
		$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
		$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
		$root->appendChild($attr);
		//childs of root
		$error = $doc->createElement('DeleteLink');
		$error= $root->appendChild($error);
		$value = $doc->createTextNode('Success');
		$value = $error->appendChild($value);
		echo $doc->saveXML();
	}
	die;

?>